package day03;

import java.util.ArrayList;

/**
 * @author jhj
 * @date 2022/5/11 15:32
 */
public class TestAlgorithm {
    public static void main(String[] args) {
        int[] a = {1,5,10,11,16,19,20,23,26,29,32};
        int d = 32;
        int left = 0,right = a.length - 1;
        int mid = a.length/2;
        int i = TestAlgorithm.foundByHalf1(a, mid,left,right,d);
        System.out.println(i);
    }
    /**
     * 二分查找练习（递归和循环2种实现）
     * 树搜索
     * 链表实现
     */
    public static int foundByHalf1(int[] d,int mid,int left,int right,int a){

        if(left == right){
            return mid;
        }

        if (d[mid]>a) {
            right = mid - 1;
            mid = left + (right - left)/2;
            return foundByHalf1(d,mid,left,right,a);
        }
        if (d[mid]<a) {
            left = mid + 1;
            mid = left + (right - left)/2;
            return foundByHalf1(d,mid,left,right,a);
        }

        return 0;
    }
}
